package com.zhanjiangyifang.shop.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhanjiangyifang.shop.utils.ToastUtils;
import com.zhanjiangyifang.shop.widget.StatueLayout;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 带有 状态页面 和 刷新加载的fragment
 * <p>
 * Created by rui on 2018/3/2.
 */

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView {
    protected P basePresenter;
    protected View mView;
    protected Context mContext;
    protected BaseActivity mActivity;

    protected boolean isVisible;
    private boolean isPrepared;
    private boolean isFirst = true;

    StatueLayout statueLayout;
    Unbinder unbinder;
    //---------------------系统方法(按顺序执行)----------------------------------
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(getLayoutId(), container, false);
        }
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null) {
            parent.removeView(mView);
        }
        basePresenter = createPresenter();
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isPrepared = true;
        unbinder = ButterKnife.bind(this, mView);
        statueLayout=setStatueLayout();
        if (isFirst) {
            initView();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = (BaseActivity) activity;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getUserVisibleHint()) {
            setUserVisibleHint(true);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        if (basePresenter != null) {
            basePresenter.detachView();
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            lazyLoad();
        }
    }

    /**
     * 懒加载
     */
    private void lazyLoad() {
        if (!isPrepared || !isVisible || !isFirst) {
            return;
        }
        initData();
        isFirst = false;
    }

    //-------------------抽象方法 子类必须实现--------------------

    /**初始化layout*/
    public abstract int getLayoutId();

    /**子类传状态控件到父类统一处理*/
    protected abstract StatueLayout setStatueLayout();

    /**createPresenter*/
    protected abstract P createPresenter();

    /**初始化view*/
    protected abstract void initView();

    /**fragment加载数据，获取数据，刷新界面*/
    protected abstract void initData();

    /*-------------------------获取宿主Activity---------------------*/
    //获取宿主Activity
    protected BaseActivity getHoldingActivity() {
        return mActivity;
    }

    //-------------------进度条--------------------
    @Override
    public void showLoadingDialog() {
        getHoldingActivity().showLoadingDialog();
    }

    @Override
    public void showLoadingDialog(String message) {
        getHoldingActivity().showLoadingDialog(message);
    }

    @Override
    public void dismissLoadingDialog() {
        getHoldingActivity().dismissLoadingDialog();
    }

    //-------------------Toast--------------------
    @Override
    public void showToast(String msg) {
        ToastUtils.showShort(getHoldingActivity(), msg);
    }

    @Override
    public void showLongToast(String msg) {
        ToastUtils.showLong(getHoldingActivity(), msg);
    }

    //-------------------公共状态--------------------
    private boolean isStatueLayoutEmpty() {
        return statueLayout != null;
    }

    @Override
    public void showLoading() {
        if (isStatueLayoutEmpty()) {
            statueLayout.showLoading();
        }
    }

    @Override
    public void showError() {
        if (isStatueLayoutEmpty()) {
            statueLayout.showError(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showLoading();
                    initData();
                }
            });
        }
    }

    @Override
    public void showError(int resId, String text) {
        if (isStatueLayoutEmpty()) {
            statueLayout.showError(resId, text, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showLoading();
                    initData();
                }
            });
        }
    }

    @Override
    public void showEmpty() {
        if (isStatueLayoutEmpty()) {
            statueLayout.showEmpty();
        }
    }

    @Override
    public void showEmpty(String text) {
        if (isStatueLayoutEmpty()) {
            statueLayout.showEmpty(text);
        }
    }

    @Override
    public void showEmpty(int resId, String text) {
        if (isStatueLayoutEmpty()) {
            statueLayout.showEmpty(resId, text);
        }
    }

    @Override
    public void hideStatueView() {
        if (isStatueLayoutEmpty()) {
            statueLayout.hide();
        }
    }

    //得到导航栏高度
    public static int getStatusBarHeight(Activity a) {
        int result = 0;
        int resourceId = a.getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            result = a.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
